from typing import List

from app.services.business.gaolu_diagnose.ind.abstract_ind import AbstractInd


class CG_LT_GL_GL04_Zha_Mg_Al(AbstractInd):

    def get_name(self):
        return "CG_LT_GL_GL04_Zha_Mg_Al"

    def get_data_source(self):
        return "mysql"

    def get_sql(self, params) -> List:
        sql_Zha_MgO = """      

        select round(avg(jyh.jyjg),3) as CG_LT_GL_GL04_Zha_MgO 
        from 
        (select jyh.*,jyt.ph, tqpd.tjsj
        from syn_zl_zl_jyh as jyh ,syn_zl_zl_jyt as jyt ,syn_zl_zl_tqpd as tqpd
        where  jyh.jcxmbm='01013' and jyh.zfbz='0' 
        and jyt.wlbm = 'ZJ003' 
        and SUBSTRING(jyt.ph, 4, 1) = '4' 
        and  jyt.jytid = jyh.jytid 
        and  jyt.ph = tqpd.ph
        and jyh.jyjg NOT LIKE '%#%' 
        and tqpd.tjsj > DATE_SUB(CURTIME(), INTERVAL 0.6 hour) ) as jyh 
                """.format(**params)

        sql_Zha_Al2O3 = """
        select round(avg(jyh.jyjg),3) as CG_LT_GL_GL04_Zha_Al2O3 
        from 
        (select jyh.*,jyt.ph, tqpd.tjsj
        from syn_zl_zl_jyh as jyh ,syn_zl_zl_jyt as jyt ,syn_zl_zl_tqpd as tqpd
        where  jyh.jcxmbm='01052' and jyh.zfbz='0' 
        and jyt.wlbm = 'ZJ003' 
        and SUBSTRING(jyt.ph, 4, 1) = '4' 
        and  jyt.jytid = jyh.jytid 
        and  jyt.ph = tqpd.ph
        and jyh.jyjg NOT LIKE '%#%' 
        and tqpd.tjsj > DATE_SUB(CURTIME(), INTERVAL 0.6 hour)   ) as jyh   
                """.format(**params)

        return [sql_Zha_MgO, sql_Zha_Al2O3]


    @AbstractInd.cache_wrapper
    def get_result(self):
        sql_list: List[str] = self.get_sql(self.params)
        cursor = self.mysql_cursor

        MgO = sql_list[0]
        cursor.execute(MgO)
        MgO = cursor.fetchone()

        MgO = MgO['CG_LT_GL_GL04_Zha_MgO'] if MgO['CG_LT_GL_GL04_Zha_MgO'] is not None else 0

        Al2O3 = sql_list[1]
        cursor.execute(Al2O3)
        Al2O3 = cursor.fetchone()
        Al2O3 = Al2O3['CG_LT_GL_GL04_Zha_Al2O3'] if Al2O3['CG_LT_GL_GL04_Zha_Al2O3'] is not None else 0

        if MgO == 0 or Al2O3 == 0: return self.get_cache()

        v = round(MgO / Al2O3, 2)
        self.set_cache(v)

        return v